#include <QtWidgets/QApplication>

#include "utils/VtkUtil.h"

#include "core/AppDirectory.h"
#include "qtmain.h"

// #define QT_MESSAGE_PATTERN = "[%{type}] %{function}:%{line} - %{message}"

void debugMessageOutput(
    QtMsgType type,
    const QMessageLogContext& context,
    const QString& msg
)
{
    Q_UNUSED(context);
    QByteArray txt = QString("").toLocal8Bit();
    // Switch structure left to be converted to write into the file in the
    // future
    switch (type) {
    case QtDebugMsg:
        txt += QString("{Debug}\t%1").arg(msg);
        break;
    case QtWarningMsg:
        txt += QString("{Warn }\t%1").arg(msg);
        break;
    case QtCriticalMsg:
        txt += QString("{Criti}\t%1").arg(msg);
        break;
    case QtFatalMsg:
        txt += QString("{Fatal}\t%1").arg(msg);
        // abort(); // deliberately core dump
    }
    QFile outFile(AppDirectory::get().getAppDocFile("debug.log"));
    outFile.open(QIODevice::WriteOnly | QIODevice::Append);

    QTextStream textStream(&outFile);
    textStream << txt << endl;
    cout << QString::fromUtf8(txt).toLocal8Bit().data() << endl;
}

int main(int argc, char* argv[])
{
    VtkUtil::hideOutputWindow();
    QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QApplication a(argc, argv);

    qInstallMessageHandler(debugMessageOutput);

    qtMain w;
    w.showMaximized();
    return a.exec();
}
